﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Reflection;
using System.ServiceModel;


using WCFMINERP.ContextPropagation;
using WCFMINERP.Contracts.DataContracts;
using WCFMINERP.SharpZipLib;
using WCFMINERP.Contracts.InterFaces;
using WCFMINERP.ComControls;
using System.Net;
using WCFMINERP.COM;
using System.IO;

namespace WCFMINERP.Update
{
    public partial class FrmLogin : Form
    {
        public bool continueInput = true;

        ChannelFactory<IShop> _proxyTransfer;//创建代理
        IShop ishop;

        public FrmLogin()
        { 
            InitializeComponent();
        }
        FrmParent frmUpload, frmOrder;

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (checkBoxRememnerUserName.Checked)
            {
                Properties.Settings.Default.IsRememberUseName = true;
                Properties.Settings.Default.UserName = txtUsername.Text;
            }
            else
            {
                Properties.Settings.Default.IsRememberUseName = false;
                Properties.Settings.Default.UserName = "";
            }
            if (checkBoxRememberPwd.Checked)
            {
                Properties.Settings.Default.IsRememberPwd = true;
                Properties.Settings.Default.UserPwd = txtPwd.Text;
            }
            else
            {
                Properties.Settings.Default.IsRememberPwd = false;
                Properties.Settings.Default.UserPwd = "";
            }
            Properties.Settings.Default.Save();
            try
            {
                _proxyTransfer = new ChannelFactory<IShop>("IShop");
                ishop = _proxyTransfer.CreateChannel();
                if (CheckData())
                {
                    string Localshacode = Properties.Settings.Default.SHACODE;
                    ShopInfo shopInfo = ishop.GetShopInfo(txtUsername.Text, Md5HashCode.GetHash(txtPwd.Text));

                    if (shopInfo != null)
                    {
                        this.Hide();
                        shopInfo.MachineName = Dns.GetHostName();

                        CheckSHACODE(shopInfo.SHACODE);
                        if (!continueInput)//检查授权码失败
                        {
                            return;
                        }

                        VersionInfo currentVersion = ishop.GetCurrentVersion(shopInfo.ShopId);
                        ShopParametersInfo paraInfo = XMLOperator.GetShopParameters(Application.StartupPath + "\\ShopParameters.xml");
                        //是否需要更新客户端版本
                        //if (Convert.ToDouble(currentVersion.ClientVersion) > Convert.ToDouble(paraInfo.ClientVersion))
                        //{
                        //    //更新客户端版本
                        //    TransferFileInfo tanFileInfo = ishop.GetFile();
                        //    FrmUpdate frmUpdate = new FrmUpdate(tanFileInfo, currentVersion.ClientVersion);
                        //    frmUpdate.ShowDialog();

                        //    ShopInfoConfig._shopInfo = shopInfo;
                        //    ShopInfoConfig.InitParameters();
                        //}
                        //是否需要更新产品信息
                        if (Convert.ToDouble(currentVersion.ShopProductVersion) > Convert.ToDouble(paraInfo.ProductVersion))
                        {
                            //更新本地产品文件

                            List<PaperFaceInfo> listPaperFaceInfo;
                            List<PaperTypeInfo> ListPaperTypeInfo;
                            List<CommonChargeItemInfo> ListCommonChargeItemInfo;
                            List<ShopProductInfo> shopProductInfoList = ishop.GetProductFile(shopInfo.ShopId, out listPaperFaceInfo, out ListPaperTypeInfo, out ListCommonChargeItemInfo);
                            XMLOperator.CreateShopProductXml(shopProductInfoList, listPaperFaceInfo, ListPaperTypeInfo, ListCommonChargeItemInfo);

                            //更新本地版本号
                            string xmlPath = Application.StartupPath + "\\ShopParameters.xml";
                            XMLOperator.UpdateNodesValue(@"ShopParameters/ProductVersion", xmlPath, currentVersion.ShopProductVersion.ToString());
                        }
                        string productxmlPath =Application.StartupPath +"\\product.xml";
                        if (!File.Exists(productxmlPath))
                        {
                            List<PaperFaceInfo> listPaperFaceInfo;
                            List<PaperTypeInfo> ListPaperTypeInfo;
                            List<CommonChargeItemInfo> ListCommonChargeItemInfo;
                            List<ShopProductInfo> shopProductInfoList = ishop.GetProductFile(shopInfo.ShopId, out listPaperFaceInfo, out ListPaperTypeInfo, out ListCommonChargeItemInfo);
                            XMLOperator.CreateShopProductXml(shopProductInfoList, listPaperFaceInfo, ListPaperTypeInfo, ListCommonChargeItemInfo);
                        }

                        ShopInfoConfig._shopInfo = shopInfo;

                        //ShopInfoConfig.InitParameters();//初始化参数

                        //显示上传界面
                        frmUpload = (FrmParent)Assembly.Load("WCFMINERP.Client").CreateInstance("WCFMINERP.Client.FrmUpload");
                        frmUpload.Shop = shopInfo;
                        frmUpload.Show();
                        ////显示开单界面
                        frmOrder = (FrmParent)Assembly.Load("WCFMINERP.Order").CreateInstance("WCFMINERP.Order.Forms.FrmOrder");
                        frmOrder.Shop = shopInfo;
                        frmOrder.Show();
                        
                    }
                    else
                    {
                        MessageBox.Show("用户名密码无效", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
            }
            catch (Exception ex)
            {
                
                MessageBox.Show("无法连接冲印车间","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                this.Close();
            }

        }

        /// <summary>
        /// 检查授权码
        /// </summary>
        /// <param name="shacode"></param>
        private void CheckSHACODE(string shacode)
        {
            string Localshacode = Properties.Settings.Default.SHACODE;
            if (Localshacode == null)
            {
                FrmSHACODE frm = new FrmSHACODE();
                frm.ShowDialog();

            }
            if (shacode != Properties.Settings.Default.SHACODE && continueInput)
            {
                MessageBox.Show("授权码无效", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                FrmSHACODE frm = new FrmSHACODE();
                frm.ShowDialog(this);
                CheckSHACODE(shacode);
            }
        }
        private bool CheckData()
        {
            bool flag = true;
            if (string.IsNullOrEmpty(txtUsername.Text.Trim()))
            {
                MessageBox.Show("请输入登录账号","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtPwd.Text.Trim()))
            {
                MessageBox.Show("请输入登录密码","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return false;
            }
            //if (string.IsNullOrEmpty(txtSHACODE.Text.Trim()))
            //{
            //    MessageBox.Show("请输入授权码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return false;
            //}
            return flag;
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.IsRememberUseName)
            {
                txtUsername.Text = Properties.Settings.Default.UserName;
                checkBoxRememnerUserName.Checked = true;
            }
            if (Properties.Settings.Default.IsRememberPwd)
            {
                txtPwd.Text = Properties.Settings.Default.UserPwd;
                checkBoxRememberPwd.Checked = true;

            }
        }

        private void ToolStripMenuItemShowOrder_Click(object sender, EventArgs e)
        {
            frmOrder.Show();
        }

        private void FrmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing && ShopInfoConfig._shopInfo!=null)
            {

                e.Cancel = true;
                this.Hide();
            }
        }

        private void ToolStripMenuItemShowUpload_Click(object sender, EventArgs e)
        {
            frmUpload.Show();
        }

        private void ToolStripMenuItemClose_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认没有单子在上传，确定退出吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                notifyIconLogin.Visible = false;
                //frmorder.notifyIcon1.Visible = false;
                Environment.Exit(0);
            }
        }

        private void linkLabelNewUser_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            FrmNewUser frm = new FrmNewUser();
            frm.ShowDialog();
        }

        private void linkLabelChangerPwd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void linkLabelChangerPwd_Click(object sender, EventArgs e)
        {
            FrmChangePwd frm = new FrmChangePwd();
            frm.ShowDialog();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        

 
    }
}
